package com.lm.security.browser;

import com.lm.security.core.properties.SecurityProperties;
import com.lm.security.core.validate.code.ValidateCodeFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * @author lm_jcf
 * @date 2018/4/27 14:12
 */
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private AuthenticationSuccessHandler lmAuthenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler lmAuthenticationFailHandler;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
//        tokenRepository.setCreateTableOnStartup(true);  //无表时自动生成token所需的表persistent_logins
        return tokenRepository;
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        ValidateCodeFilter filter = new ValidateCodeFilter();
        filter.setAuthenticationFailureHandler(lmAuthenticationFailHandler);
        filter.setSecurityProperties(securityProperties);
        filter.afterPropertiesSet();

        http.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class)
                .formLogin()  //表单方式
                .loginPage("/authentication/require")
                .loginProcessingUrl("/authentication/form")
                .successHandler(lmAuthenticationSuccessHandler)
                .failureHandler(lmAuthenticationFailHandler)
//        http.httpBasic()    //弹框方式
                .and()
                .rememberMe()
                    .tokenRepository(persistentTokenRepository())
                    .tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())
                    .userDetailsService(userDetailsService)
                .and()
                .authorizeRequests()    //授权
                .antMatchers("/authentication/require",
                        "/code/*",
                        securityProperties.getBrowser().getLoginPage()).permitAll() //此表示，此链接不需要身份验证
                .anyRequest()       //身份认证
                .authenticated()
                .and()
                .csrf().disable();

    }
}
